 
package tablization;

import java.sql.ResultSet;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;
import utility.Input;
import dbmanager.DBManager;
import dbmanager.GetIdName;
import file.SearchRecord;

/**
 *  @author Shweta
 **/
public class SearchingPropeties extends SubTable
{
		
	public SearchingPropeties(DBManager sqlDB) 
	{
		super.setDBManager(sqlDB);
	}
	
		
	/**
	 * this function returns string message as per tablename structure
	 * @param tablename - name of table in whose structure message is required
	 * @param process - name of process in whose structure message is required
	 * @param hashtable - containing values to be converted in string format in given structure
	 * @return string containing message in a given format
	 */
	private String convertToMessage(String tablename, String process,Hashtable<Object,Object> hashtable) 
	{
		
		String message = "", columnSpchar ="", valueSpchar = "";
		
		StringBuffer strbfr = new StringBuffer();
		String query = "select spchar from command where msgtype='"+tablename+"' and process = '"+process+"' and order1>0 order by order1";
		ResultSet rs = sqlDB.getSelect(query);
		
		try
		{
			rs.next();
			columnSpchar = rs.getString(1);
			rs.next();
			valueSpchar = rs.getString(1);
			
			Enumeration<Object> enumt = hashtable.keys();
			while(enumt.hasMoreElements())
			{
				String column = enumt.nextElement().toString();
				String value = hashtable.get(column).toString();
			
				strbfr.append(columnSpchar);
				strbfr.append(column);
				strbfr.append(" ");
				strbfr.append(valueSpchar);
				strbfr.append(value);
				strbfr.append(" ");
				
			}
			
			message = strbfr.toString();
		}
		catch (Exception e) {
			// TODO: handle exception
		}
						
				 
		return message;
	}
	
	

	
	/**
	 * this function gives name of table for given object respective to given columns 
	 * @param tablearray -array of tables belonging to particular object name
	 * @param colarray -array of column names as per given condition
	 * @return string containing name of table 
	 */
	public String getTableName(String [] tablearray,String [] colarray)
	{
		
		String tablename = "" ;
		int size = colarray.length;
		int counter = 0;
		
		for (int i = 0; i < tablearray.length; i++) 
		{
			
			String strId = tablearray[i].split(Input.SEPARATOR)[0];
			String strName = gin.getItem(strId);
			
			String path = Input.MASTERREPORTSPATH + strId + "//" + strId + "_mid" + Input.FILRFORMAT;
			sr = new SearchRecord(path);
			 
			 for (int j = 0; j < size; j++)
			 {
				 String value = gin.getId(colarray[j]);
				 
				 Hashtable<Object,Object> hash = new Hashtable<Object, Object>();
				 hash.put("pv",value);
				 Hashtable<Object,Object> hasht = sr.getRecordHash(hash);
				 if(hasht != null)
				 {
					 counter++;
				 }		
			 }			 
			 
			 if(size == counter)
			 {
				 tablename = strName;
				 break;
			 }
			
		}
			
		return tablename;
		
	}
	
	
	/**
	 * this function works like select function of database & displays in a string format
	 * @param hashtable - containing conditions on which selection to be done
	 * @return - string containing message format of records from tables as per given conditions  
	 */
	@SuppressWarnings("unchecked")
	public String getView(Hashtable<Object,Object> hashtable,String type)
	{

		Hashtable<Object, Object> tempHash = hashtable;	
		
		int index=0;
		String message = "", masterName ="", name="";	
		String [] columnArray ; 
	
		//String [] tableArray = getObjectTableArray(type);
		
		Hashtable<Object,Object> arrayHash = new Hashtable<Object, Object>();				
		
		arrayHash.put(PID_NAME, "object");
		arrayHash.put(keyName, "pv");
		arrayHash.put(valueName, gin.getId(type));
		
		String [] tableArray= getArray(arrayHash);	
		
		columnArray = new String[tempHash.size()];
		
		Enumeration<Object> enumt = tempHash.keys();
		while (enumt.hasMoreElements())
		{
			String columnName = (String) enumt.nextElement();
			columnArray[index] = columnName;		
			index++;
		}
		
		String tablename = getTableName(tableArray,columnArray);
		
		if(!tablename.equals(""))
		{
			
			Vector<Object> detailVector  = new Vector<Object>();
				
			detailVector.addAll(sqlDB.getSelectVector(tempHash, tablename));	
		
			if(!detailVector.isEmpty())
			{
				StringBuilder strbdr = new StringBuilder();		
			
				for(int i=0;i<detailVector.size();i++)
				{
					Hashtable<Object,Object> hasht = (Hashtable<Object, Object>)detailVector.get(i);	
					name = gin.getItem(hasht.get("id").toString());
					if(!masterName.equalsIgnoreCase(name))
					{			
						strbdr.append(Input.ENDSEPARATOR);
						strbdr.append(name);
						masterName  = name;
					}
					
					hasht.remove("id");
					//changes hashtable into string message format
					String master=convertToMessage("property_details","add", hasht);				
					strbdr.append(master);				
				}
				message = strbdr.toString();
			}
		}
		
		 return message;
		
	}
	
		
	public static void main(String[] args) 
	{
		DBManager sqlDB = new DBManager();
		sqlDB.getConnect();
		
		GetIdName gin = new GetIdName(sqlDB);
		SearchingPropeties sp = new SearchingPropeties(sqlDB);
		
		Hashtable<Object,Object> hasht = new Hashtable<Object,Object>();	
		
		hasht.put("id",gin.getId("Mansa"));
		hasht.put("school","DAV");		
		
		String type = "student";
		String message = "";
		message = sp.getView(hasht,type);
		if(!message.equals(""))
			System.out.println(message);
		else
			System.out.println("no such detail exists");
		
	}
	
}